package com.wangshidai.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wangshidai.entity.R;
import com.wangshidai.entity.SysPermission;
import com.wangshidai.entity.ztree.TreeNode;
import com.wangshidai.entity.ztree.TreeNode1;
import com.wangshidai.form.permissionForm.SavePermissionForm;
import com.wangshidai.form.permissionForm.UpdatePermissionForm;
import com.wangshidai.service.SysPermissionService;
import com.wangshidai.service.SysRolePermissionService;
import com.wangshidai.util.ztree.ListToTree;
import com.wangshidai.util.ztree.ListToTreeImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Permission")
@Api(tags = "权限管理")
public class PermissionController1 {

    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @GetMapping("/queryPermission")
    @ApiOperation("查询所有权限")
    public R queryPermission(){
        //新增集合
        List<TreeNode> list = new ArrayList();
        //调用服务层查询方法，查询所有权限
        List<SysPermission> sysPermissionList = sysPermissionService.querySysPerssionList();

        //循环取出集合中的每一个对象
        for (SysPermission sysPermission : sysPermissionList) {
            TreeNode treeNode=new TreeNode();
            //并进行赋值
            BeanUtil.copyProperties(sysPermission,treeNode);
            list.add(treeNode);
        }
        ListToTree treeBuilder = new ListToTreeImpl();
        List<TreeNode> tree = treeBuilder.toTree(list);
        System.out.println(tree);
        return new R(200,"查询成功",tree);
    }

    @GetMapping("/queryPreviousMenu")
    @ApiOperation("查询上级菜单")
    public R queryPreviousMenu(){
        List<TreeNode1> list = new ArrayList();
        //查询所有上级菜单
        List<SysPermission> sysPermissionList = sysPermissionService.queryPreviousMenu();

        for (SysPermission sysPermission : sysPermissionList) {
            TreeNode1 treeNode1=new TreeNode1();
            BeanUtil.copyProperties(sysPermission,treeNode1);
            list.add(treeNode1);
        }
        ListToTree treeBuilder = new ListToTreeImpl();
        List<TreeNode> tree = treeBuilder.toTree(list);
        System.out.println(tree);
        return new R(200,"查询成功",tree);
    }

    @PostMapping("/savePermission")
    @ApiOperation("新增权限")
    public R savePermission(SavePermissionForm savePermissionForm){
        SysPermission sysPermission=new SysPermission();
        //赋值
        BeanUtil.copyProperties(savePermissionForm,sysPermission);
        //判断如果服务层返回true则新增成功,反之添加失败
        Boolean state = sysPermissionService.savePermission(sysPermission);
        if (state) {
            return new R(200, "新增成功", null);
        }
        return new R(-200, "新增失败", null);
    }

    @PutMapping("/updatePermission/{id}")
    @ApiOperation("修改权限")
    @ApiImplicitParam(name = "id",value = "权限id")
    public R updatePermission(UpdatePermissionForm updatePermissionForm, @PathVariable Integer id){
        //先根据id进行查询看数据是否存在
        SysPermission sysPermission = sysPermissionService.queryById(id);
        if (sysPermission == null) {
            return new R(-200, "数据不存在，修改失败", null);
        }
        //如果存在进行赋值
        BeanUtil.copyProperties(updatePermissionForm,sysPermission);
        //服务层返回true则进行修改，并返回修改成功
        sysPermissionService.updatePermission(sysPermission);
        return new R(200, "修改成功", null);

    }

    @DeleteMapping("/deletePermission/{id}")
    @ApiImplicitParam(name = "id",value = "权限id")
    @ApiOperation("删除权限")
    public R deletePermission(@PathVariable Integer id){
        //根据id进行数据删除
        sysPermissionService.deletePermission(id);
        //把sys_permission表中idsys_role_permission表中permission_id相同数据进行删除
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("permission_id",id);
        //调用服务层方法进行删除
        sysRolePermissionService.remove(wrapper);
        return new R(200, "删除成功", null);
    }
}
